class_name EnemyEntityState_Common
extends EnemyEntityState

@export var anim_name:String = ""
@export var additional_effects:Array[EntityStateAnim_AdditionalEffect]
var reses:Array[Util.AddCallMethodTrackRes] = []
var is_anim_finished:bool = false
var additional_effects_dict:Dictionary = {}
var excute_idx:int = 0
signal state_entered
signal state_exited

func on_anim_finished(anim_name:String):
	if self.anim_name == anim_name:
		is_anim_finished = true
func state_init(fsm:FSM):
	super.state_init(fsm)
	excute_idx = 0
	for timing in GameEnum.EntityStateAnim_AdditionalEffect_Timing.keys():
		additional_effects_dict[GameEnum.EntityStateAnim_AdditionalEffect_Timing[timing]] = []
	for effect in additional_effects:
		additional_effects_dict[effect.timing].append(effect)
	for effect in additional_effects_dict[GameEnum.EntityStateAnim_AdditionalEffect_Timing.Init]:
		effect.init_additional_effect(self)
		reses.append_array(effect.excute())
func on_enter(from_state:State):
	super.on_enter(from_state)
	is_anim_finished = false
	var anim:Animation = controller.get_animation(anim_name)
	reses.clear()
	controller.add_anim_finished_event(anim_name,on_anim_finished,CONNECT_ONE_SHOT)
	for effect in additional_effects_dict[GameEnum.EntityStateAnim_AdditionalEffect_Timing.Enter]:
		effect.init_additional_effect(self)
		reses.append_array(effect.excute())
	for effect in additional_effects_dict[GameEnum.EntityStateAnim_AdditionalEffect_Timing.Time]:
		if effect.time == 0:
			effect.init_additional_effect(self)
			reses.append_array(effect.excute())
		elif anim:
			reses.append(Util.add_call_method_track_and_method(controller.cur_entity.animation_player,anim_name,effect.time,"on_additional_effect_excute",[excute_idx],self))
		excute_idx += 1
	controller.play_animation(anim_name)
	state_entered.emit()
func on_update(delta:float):
	super.on_update(delta)
	for effect in additional_effects_dict[GameEnum.EntityStateAnim_AdditionalEffect_Timing.Update]:
		effect.init_additional_effect(self)
		reses.append_array(effect.excute())
func on_physics_update(delta:float):
	super.on_physics_update(delta)
	for effect in additional_effects_dict[GameEnum.EntityStateAnim_AdditionalEffect_Timing.Physics_Update]:
		effect.init_additional_effect(self)
		reses.append_array(effect.excute())
func on_exit(to_state:State):
	for effect in additional_effects_dict[GameEnum.EntityStateAnim_AdditionalEffect_Timing.Exit]:
		effect.init_additional_effect(self)
		reses.append_array(effect.excute())
	state_exited.emit()
	reses.reverse()
	for res in reses:
		res.remove_track.call()
	reses.clear()
	excute_idx = 0
	super.on_exit(to_state)
func get_state_status() -> GameEnum.StateStatus:
	return super.get_state_status()
func get_additional_effect_by_idx(idx:int) -> EntityStateAnim_AdditionalEffect:
	if idx < additional_effects.size():
		return additional_effects[idx]
	return null
func on_additional_effect_excute(idx:int):
	if idx < additional_effects.size():
		var effect = additional_effects[idx]
		effect.init_additional_effect(self)
		reses.append_array(effect.excute())

func _init() -> void:
	additional_effects = additional_effects.duplicate(true)
